/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xinxi.dms.service;

import com.xinxi.dms.dao.BaseDao;
import com.xinxi.dms.dao.CheckinDao;
import com.xinxi.dms.dao.RoomDao;
import com.xinxi.dms.dao.StudentDao;
import com.xinxi.dms.po.Room;
import com.xinxi.dms.po.Stay;
import com.xinxi.dms.po.Student;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author zhigu
 */
public class CheckoutServiceIMPL implements ICheckoutService{
    CheckinDao checkoutDao = new CheckinDao();
         RoomDao roomDao = new RoomDao();
      StudentDao studentDao = new StudentDao();
    @Override
    public List<Stay> findAll_25(String roomno) {
        String sql ="select s.stuid,s.stuname,s.stusex,s.stuschool,s.state,"
                + "r.roomid,r.buildingno,r.unitno,r.roomno from student "
                + "as s,room as r where s.buildingno = r.buildingno and "
                + "s.unitno=r.unitno and s.roomno =r.roomno and "
                + "r.roomno = ? and r.buildingno = 25";
        Object[] params = {roomno};
        return checkoutDao.query(sql, Stay.class, params);
    }

    @Override
    public boolean checkout(Integer stuid, Integer roomid) {
        String sql_1 = "update student set state = '已退宿' where stuid = ?";
        Object[] params_1 = {stuid};

        String sql_2 = "insert into stay(stuid,roomid,checkouttime,checkoutstate) "
                + "values(?,?,now(),?)";
        Object[] params_2 = {stuid, roomid, "退宿"};

        String sql_3 = "select remarks,checknumber from room where roomid = ?";
        Object[] params_3 = {roomid};

        //修改房间的入住人数 和 房间状态
        String sql_4 = "update room set checknumber = checknumber-1 , state = ? where roomid = ?";
        Object[] params_4 = {"未开放", roomid};

        String sql_5 = "update room set checknumber = checknumber-1 where roomid = ?";
        Object[] params_5 = {roomid};
        boolean result = true;
        Connection conn = new BaseDao().getConnection();
         try {
            //开启事务
            conn.setAutoCommit(false);
            //先执行查询房间所剩人数
            Room room = roomDao.get(sql_3,Room.class, params_3);
            if(room.getChecknumber().equals(1))
                //修改房间状态并入住人数-1
                roomDao.update(conn, sql_4, params_4);
            else
                roomDao.update(conn, sql_5, params_5);
            studentDao.update(conn, sql_1, params_1);
            roomDao.update(conn, sql_2, params_2);
            conn.commit();
        } catch (SQLException ex) {
            try {
                //事务回滚
                result =false;
                conn.rollback();
                //事务回滚
            } catch (SQLException ex1) {
                Logger.getLogger(CheckinServiceIMPL.class.getName()).log(Level.SEVERE, null, ex1);
            }
            Logger.getLogger(CheckinServiceIMPL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public Student getState(int stuid) {
    String sql = "select state from student where stuid = ?";
       Object[] params = {stuid};
       return checkoutDao.get(sql,Student.class, params);   
    }

    @Override
    public List<Stay> findAll_23(String roomno) {
        String sql ="select s.stuid,s.stuname,s.stusex,s.stuschool,s.state,"
                + "r.roomid,r.buildingno,r.unitno,r.roomno from student "
                + "as s,room as r where s.buildingno = r.buildingno and "
                + "s.unitno=r.unitno and s.roomno =r.roomno and "
                + "r.roomno = ? and r.buildingno = 23";
        Object[] params = {roomno};
        return checkoutDao.query(sql, Stay.class, params);
    }

    
}
